<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <style type="text/css">
        .aui-list .aui-list-item-media {
            width: 3rem;
        }
        .notes-add {
            position: fixed;
            left: 0.5rem;
            bottom: 0.5rem;
            width: 3rem;
            z-index: 99;
        }
        textarea {
            height: 8rem;
            background-color: #ffffff;
            padding: 0.5rem;
        }
        .photos img {
            display: block;
            width: 100%;
        }
        .add-photos > div {
            width: 100%;
            height:5.15rem;
            line-height: 5.15rem;
        }
        .add-photos > div .aui-iconfont {
            font-size: 2rem;
            color: #ccc;
        }
        .image-item {
            position: relative;
            height: 5.3rem;
            overflow: hidden;
            background-color: #f0f0f0;
        }
        .image-item img {
            display: block;
            margin: 0 auto;
            width: auto;
            height: 100%;
        }
        .image-item .delete-btn {
            position: absolute;
            left: 50%;
            top: 50%;
            width: 28px;
            height: 28px;
            background-color: rgba(0,0,0,0.7);
            margin-left: -14px;
            margin-top: -14px;
            color: #ffffff;
            text-align: center;
            border-radius: 50%;
        }
    </style>
</head>
<body>
    <p class="aui-margin-t-15 aui-margin-l-15"><span>现在时间:&nbsp;</span><span id="nowTime">2017-07-27 09:10:45</span></p>
    <section class="aui-content-padded">
        <textarea placeholder="请填写签到说明..." id="remarks"></textarea>
    </section>
    <section class="aui-content-padded aui-clearfix">
        <div class="aui-info">
            <div class="aui-info-item aui-padded-r-10" style="width: 13rem">
                <div>
                    <i class="aui-iconfont aui-icon-location"></i>
                    <div id="location"></div>
                </div>
            </div>
            <div class="aui-info-item" style="width: 80px;" tapmode onclick="reloadLocation()">
                <i class="aui-iconfont aui-icon-refresh aui-text-info aui-margin-r-5"></i>
                <span class="aui-text-info">重新定位</span>
            </div>
        </div>
        <!-- <div class="aui-info">
            <div class="aui-info-item aui-padded-r-10">
                <div id="location" class="aui-ellipsis"></div>
            </div>
        </div> -->
    </section>
    <div class="aui-content aui-margin-15">
        <div class="aui-btn aui-btn-info aui-btn-block aui-btn-sm" onclick="sign(1)">签到&nbsp;&nbsp;&nbsp;&nbsp;<font id="id_start_time"></font></div>
    </div>
    <div class="aui-content aui-margin-l-15 aui-margin-r-15">
        <div class="aui-btn aui-btn-danger aui-btn-block aui-btn-sm" onclick="sign(2)">签退&nbsp;&nbsp;&nbsp;&nbsp;<font id="id_end_time"></font></div>
    </div>

</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/zepto.min.js" ></script>
<script type="text/javascript" src="../script/aui-toast.js" ></script>
<script type="text/javascript">
    var toast = new auiToast();
    apiready = function(){
        api.ajax({
            url: ServeUrl + 'm=Index&a=workTime',
            method: 'post',
            cache : false,
            timeout : 30,
            dataType : 'json',
            data : {
                values : {
                    username : $api.getStorage("username"),
                }
            }

        },function(ret, err){
            // alert(JSON.stringify(ret))
            if(ret.sign=='Y'){
                $("#id_start_time").parent("div").parent("div").remove();
            }
            if (ret.signout == 'Y') {
                $("#id_end_time").parent("div").parent("div").remove();
            }
            $("#id_start_time").html(ret.start_work_time);
            $("#id_end_time").html(ret.end_work_time);
            
        });
        getNameFromCoords();
        start();

    };
    function closeWin(){
        api.closeWin({
        });
    }

    function start() {
        var today=new Date();
        var year=today.getFullYear();
        var month=today.getMonth()+1;
        var day=today.getDate();
        var hours=today.getHours();
        var minutes=today.getMinutes();
        var seconds=today.getSeconds();
        //如果是单位数字，前面补0
        month=month<10? "0"+month :month;
        day=day<10? "0"+day :day;
        hours=hours<10? "0"+hours :hours;
        minutes=minutes<10? "0"+minutes :minutes;
        seconds=seconds<10? "0"+seconds :seconds;
        //时间信息连成字符串
        var str=year+"-"+month+"-"+day+"- "+hours+":"+minutes+":"+seconds;
        //获取id=result的内容
        var obj=document.getElementById("nowTime");
        obj.innerHTML=str;
        //延时器
        setTimeout("start()",1000);
    }

    function reloadLocation() {
        var bMap = api.require('bMap');
        //alert(bMap);
        bMap.getLocation({
            accuracy : '100m',
            autoStop : true,
            filter : 1
        }, function(ret, err) {
            // alert(ret.lat)
            if (ret.status) {
                testGetCity(ret.lat, ret.lon);
                toast.success({
                    title:"重新定位成功!",
                    duration:1500
                });
                api.hideProgress();
            } else {
                alert(JSON.stringify(err))
            }
        });
    }

    function getNameFromCoords() {
        var bMap = api.require('bMap');
        // alert(1)
        bMap.getLocation({
            accuracy : '100m',
            autoStop : true,
            filter : 1
        }, function(ret, err) {
            // alert(ret)
            if (ret.status) {
                testGetCity(ret.lat, ret.lon)
            } else {
                alert(JSON.stringify(err))
            }
        });

    }

    function testGetCity (lat, lon) {
        var local = lat +','+ lon;
        // alert(local)
        api.ajax({
            url : "http://api.map.baidu.com/geocoder/v2/",
            timeout : 0,
            method : "post",
            headers : {
                "Content-type" : "application/x-www-form-urlencoded"
            },
            data : {
                values : {
                    "ak" : "ucEWxPeE47LPggDLBfE8b11YklfqVHXy",
                    "callback" : "renderReverse",
                    "location" : local,
                    "output" : "json",
                    "mcode" : "0B:13:25:D7:85:46:0A:67:12:F3:29:88:64:56:63:10:7A:9C:C4:59;com.r897332330.zkc",
                    pois : 0,
                }
            }
        }, function(ret, err){
            if (ret) {
                var place = ret.result.addressComponent.city+ret.result.addressComponent.district+ret.result.addressComponent.street
                var sematic_description = ret.result.sematic_description;
                // alert(place);
                $('#location').html(place+" "+sematic_description);
                $api.setStorage('place', place);
                $api.setStorage('sematic_description', sematic_description);
                // alert(JSON.stringify(ret))
            } else {
                alert(JSON.stringify(err))
                // alert(err)
            }
        })
    }


    var signNum = false;
    function sign (signout) {
        var remarks = $('#remarks').val();
        // 提交成功 在窗口关掉前阻止再次提交
        if (signNum) return false;
        var place = $api.getStorage("place");
        var sematic_description = $api.getStorage("sematic_description");
        // if (place == '') return false;
        if (signout == 2) {
            var strSignout = '已签退';
        } else {
            var strSignout = '已签到';
        }
        // 加载内容
        api.ajax({
            url: ServeUrl + 'm=Sign&a=sign',
            method: 'post',
            data: {
                values: {
                    username: $api.getStorage("username"),
                    place : place,
                    sematic_description : sematic_description,
                    remarks : remarks,
                    signout : signout,
                },
            },
            dataType : 'json',
        }, function(ret, err) {
            if (ret.status == 1) {
                toast.success({
                    title:strSignout,
                    duration:2000
                });
                api.hideProgress();
                // 提交成功 在窗口关掉前阻止再次提交
                signNum = true;

                setTimeout('api.closeWin()', 1500)
            } else {
                toast.fail({
                    title:"签到失败",
                    duration:2000
                });
                api.hideProgress();
            }
        });
    }
</script>
</html>
